package com.sg.plmadapter.handler;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import com.mobnut.commons.util.Utils;
import com.sg.business.model.Folder;
import com.sg.business.model.User;
import com.sg.business.model.toolkit.UserToolkit;
import com.sg.widgets.part.CurrentAccountContext;

public class VistePLMFolder extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IStructuredSelection is = (IStructuredSelection) HandlerUtil
				.getCurrentSelection(event);
		if (!is.isEmpty()) {
			Object firstElement = is.getFirstElement();
			if (firstElement instanceof Folder) {
				Folder folder = (Folder) firstElement;
				if (!folder.isContainer()) {
					Map<String, String> parameter = new HashMap<String, String>();
					parameter.put("folderOid",
							folder.getPLMObjectInformationOid());
					CurrentAccountContext context = new CurrentAccountContext();
					String uid = context.getConsignerId();
					User user = UserToolkit.getUserById(uid);
					parameter.put("username", user.getUserid());
					parameter.put("password", (String) user.getValue("adpsw"));
					Utils.openURL(folder.getPLMObjectInformationURL(),
							parameter);
				}
			}
		}
		return null;
	}

}
